package com.hdax.dmw.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    //交换器
    @Value("${dm.order.exchange}")
    private String order_exchange;
    //订单队列
    @Value("${dm.order.queue}")
    private String order_queue;
    //订单路由
    @Value("${dm.order.routingKey}")
    private String order_routing_key;
    //死信
    @Value("${dm.order.dlx.exchange}")
    private String order_dlx_exchange;
    //订单队列
    @Value("${dm.order.dlx.queue}")
    private String order_dlx_queue;
    //订单路由
    @Value("${dm.order.dlx.routingKey}")
    private String order_dlx_routing_key;

    //创建业务队列
    @Bean
    public DirectExchange orderExchange(){
        return new DirectExchange(order_exchange);
    }
    @Bean
    public Queue orderQueue(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange",order_dlx_exchange);
        map.put("x-dead-letter-routing-key",order_dlx_routing_key);
        return new Queue(order_queue,true,false,false,map);
    }

    @Bean
    public Binding orderBinding(){
        return BindingBuilder
                .bind(orderQueue())
                .to(orderExchange())
                .with(order_routing_key);
    }

    @Bean
    public DirectExchange dlxExchange(){
        return new DirectExchange(order_dlx_exchange);
    }

    @Bean
    public Queue dlxQueue(){
        return new Queue(order_dlx_queue);
    }

    @Bean
    public Binding dlxBinding(){
        return BindingBuilder
                .bind(dlxQueue())
                .to(dlxExchange())
                .with(order_dlx_routing_key);
    }
















}
